package myky.com.doctorapp.utils;

import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import myky.com.doctorapp.entity.DownLoadFileEntity;
import myky.com.doctorapp.utils.dbhelper.DbHelper;
import myky.com.doctorapp.utils.dbhelper.DownLoadTask;
import myky.com.doctorapp.utils.dbhelper.OnProgressListener;

/**
 * Created by Mr.li on 2018/7/16.
 */

public class DownLoaderManger {
    //文件下载存储位置
    public static String FILE_PATH = Environment.getExternalStorageDirectory() + "/Cmai";
    //数据库帮助类
    private DbHelper mHelper;
    private SQLiteDatabase db;
    //进度回调监听
    private OnProgressListener mListener;
    //保存正在下载的任务信息
    private Map<String, DownLoadFileEntity> map = new HashMap<>();
    private static DownLoaderManger manger;

    public DownLoaderManger(DbHelper helper, OnProgressListener listener) {
        mHelper = helper;
        mListener = listener;
        db = helper.getReadableDatabase();
    }

    /**
     * 单例模式
     *
     * @param helper   数据库帮助类
     * @param listener 下载进度回调接口
     * @return
     */
    public static synchronized DownLoaderManger getInstance(DbHelper helper, OnProgressListener listener) {
        if (manger == null) {
            synchronized (DownLoaderManger.class) {
                if (manger == null) {
                    manger = new DownLoaderManger(helper, listener);
                }
            }
        }
        return manger;
    }

    /**
     * 开始下载任务
     */
    public void start(String url) {
        db = mHelper.getReadableDatabase();
        DownLoadFileEntity info = mHelper.queryData(db, url);
        map.put(url, info);
        //开始任务下载
        new DownLoadTask(map.get(url), mHelper, mListener).start();
    }

    /**
     * 停止下载任务
     */
    public void stop(String url) {
        db = mHelper.getWritableDatabase();
        map.get(url).setStop(true);
        mHelper.updataData(db, map.get(url));
        //db.close();
    }

    /**
     * 查询任务进度
     */
    public DownLoadFileEntity checkData(String url) {
        db = mHelper.getReadableDatabase();
        DownLoadFileEntity info = mHelper.queryData(db, url);
        return info;
    }

    /**
     * 更新任务进度
     */
    public void update(DownLoadFileEntity info) {
        db = mHelper.getWritableDatabase();
        mHelper.updataData(db,info);
    }


    /**
     * 重新下载任务
     */
    public void restart(String url) {
        stop(url);
        try {
            File file = new File(FILE_PATH, map.get(url).getFileName());
            if (file.exists()) {
                file.delete();
            }
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        db = mHelper.getWritableDatabase();
        mHelper.resetData(db, url);
        start(url);
    }

    /**
     * 获取当前任务状态
     */
    public boolean getCurrentState(String url) {
        return map.get(url).isDownLoading();
    }

    /**
     * 添加下载任务
     *
     * @param info 下载文件信息
     */
    public void addTask(DownLoadFileEntity info) {
        //判断数据库是否已经存在这条下载信息
        if (!mHelper.isExist(db, info)) {
            //不存在
            db = mHelper.getWritableDatabase();
            mHelper.insertData(db, info);
            map.put(info.getUrl(), info);
        } else {
            //存在
            //从数据库获取最新的下载信息
            db = mHelper.getReadableDatabase();
            DownLoadFileEntity fileInfo = mHelper.queryData(db, info.getUrl());
            if (!map.containsKey(info.getUrl())) {
                map.put(info.getUrl(), fileInfo);
            }
        }
    }

    /**
     * 删除下载任务
     *
     * @param info 下载文件信息
     */
    public void deleteTask(DownLoadFileEntity info, int type) {
            //判断数据库是否已经存在这条下载信息
            db = mHelper.getWritableDatabase();
            mHelper.deleteData(db, info);
            if (type == 1){
                db.close();
            }
    }

}
